package com.oneonefive.educationsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneonefive.educationsystem.domain.StudentRequest;
import com.oneonefive.educationsystem.domain.User;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.mapper.StudentRequestMapper;
import com.oneonefive.educationsystem.service.SemesterService;
import com.oneonefive.educationsystem.service.StudentRequestService;
import com.oneonefive.educationsystem.utils.Constant;
import com.oneonefive.educationsystem.utils.StateChangeUtil;
import com.oneonefive.educationsystem.vo.SemesterVo;
import com.oneonefive.educationsystem.vo.StudentRequestVo;
import io.netty.util.internal.StringUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


@Service
public class StudentRequestServiceImpl extends ServiceImpl<StudentRequestMapper, StudentRequest>
    implements StudentRequestService{

    @Resource
    SemesterService semesterService;

    @Resource
    StudentRequestMapper studentRequestMapper;

    @Override
    public Result stuReqList(StudentRequestVo studentRequestVo) {
        List<StudentRequestVo> list;
        list=studentRequestMapper.stuReqListCount(studentRequestVo);
        long total = list.size();
        if (studentRequestVo.getPage() != null) {
            studentRequestVo.setPage((studentRequestVo.getPage()-1)*10);
            list = studentRequestMapper.stuReqList(studentRequestVo);
        }
        List<StudentRequestVo> resList = getResVo(list);
        return Result.ok(resList, total);
    }


    public List<StudentRequestVo> getResVo(List<StudentRequestVo> list) {
        List<StudentRequestVo> resList = new ArrayList<>();
        for (StudentRequest studentRequest : list) {
            StudentRequestVo studentRequestVo = new StudentRequestVo();
            BeanUtils.copyProperties(studentRequest, studentRequestVo);
            studentRequestVo.setStateString(StateChangeUtil.stuReqStateChange(studentRequest.getState()));
            studentRequestVo.setSectionString(StateChangeUtil.sectionChange(studentRequest.getSection()));
            resList.add(studentRequestVo);
        }
        return resList;
    }

    @Override
    public Result legitimacy(StudentRequest studentRequest) {
        if (!Pattern.matches(Constant.TERM_REGEX, studentRequest.getTerm())) {
            return Result.fail("输入的学期不符合格式");
        }
        if (studentRequest.getLabId().length() > 10) {
            return Result.fail("实验室编号过长");
        }
        if (studentRequest.getReason().length() > 200) {
            return Result.fail("申请原因过长");
        }
        if (studentRequest.getWeek() < 0) {
            return Result.fail("周次不能为负");
        }
        SemesterVo semesterVo = new SemesterVo();
        semesterVo.setTerm(studentRequest.getTerm());
        Result result = semesterService.semesterList(semesterVo);
        if (result.getTotal() == 0) {
            return Result.fail("输入的学期不存在");
        }
        semesterVo = ((List<SemesterVo>) result.getData()).get(0);
        if (studentRequest.getWeek() > semesterVo.getWeek()) {
            return Result.fail("周次不存在");
        }
        if (studentRequest.getSection() < 0 || studentRequest.getSection() > 5) {
            return Result.fail("节次不存在");
        }
        return Result.ok();
    }

    @Override
    public Result setValue(StudentRequest studentRequest, User user) {

        System.out.println(Constant.SEMESTER);
        if (StringUtil.isNullOrEmpty(studentRequest.getTerm())) {
            if (Constant.SEMESTER == null) {
                return Result.fail("未设置当前学期");
            }
            studentRequest.setTerm(Constant.SEMESTER.getTerm());
        }
        if (studentRequest.getUserId() == null) {

            studentRequest.setUserId(user.getUserId());
        }
        return Result.ok("term和userid未丢失");
    }

}




